var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/util/common.ts
var common_exports = {};
__export(common_exports, {
  CommonUtil: () => CommonUtil
});
module.exports = __toCommonJS(common_exports);
var import_iup_core = require("@newgrand/iup-core");
function openUrl(url, title) {
  if (title) {
    url = import_iup_core.core.handleUrl(url, { appTitle: encodeURIComponent(title) });
  }
  url = import_iup_core.core.getAbsoluteUrl(url);
  const btn = document.createElement("a");
  btn.target = "_blank";
  btn.href = url;
  btn.click();
}
var CommonUtil = {
  /**
   * 在列表页面注册回刷事件
   * @param busType 业务类型
   * @param callback 回刷事件
   */
  onRefreshList(busType, callback) {
    var _a;
    const unSets = ((_a = (0, import_iup_core.getCurrentWindow)("currentPageInstance")) == null ? void 0 : _a.unSubscribeFn) || /* @__PURE__ */ new Set();
    const un = import_iup_core.core.getObserver().subscribe(callback, `refresh${busType}`);
    unSets.add(un);
    return () => {
      un();
      unSets.has(un) && unSets.delete(un);
    };
  },
  /**
   * 在编辑页面回刷列表页面
   * @param busType 业务类型
   * @param data 回刷数据值
   */
  refreshList(busType, data) {
    try {
      import_iup_core.core.getObserver().notify(data, `refresh${busType}`).then();
    } catch (e) {
      console.log(e);
    }
  },
  /**
   * 获取public文件夹下的路径，微前端下会自动拼接微前端地址
   * @param url
   */
  getPublicPath(url) {
    var _a, _b, _c;
    const prefix = ((_c = (_b = (_a = import_iup_core.core.external).getQianKun) == null ? void 0 : _b.call(_a)) == null ? void 0 : _c.publicPath) || "./";
    if (url.indexOf("/") === 0) {
      url = url.substring(1);
    }
    return prefix + url;
  },
  /**
   *
   * @param path
   * @param queryParmas  microAppName 微应用名
   * @returns
   */
  open(path, queryParmas = {}) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
    const tmpPath = /^https?:/.test(path) ? path : path.indexOf("/") === 0 ? path : `/${path}`;
    const { microAppName: appName, ...query } = queryParmas || {};
    if (query.hasOwnProperty("AppTitle")) {
      query.appTitle = query.AppTitle;
      delete query.AppTitle;
    }
    let subRoot = ((_b = (_a = import_iup_core.core.external).getQianKun) == null ? void 0 : _b.call(_a).subRoot) || "/sub";
    if (subRoot) {
      subRoot = subRoot.indexOf("/") === 0 ? subRoot : `/${subRoot}`;
    }
    let microAppName = appName || (tmpPath.indexOf(`${subRoot}/`) === 0 ? tmpPath.split("/")[2] : appName);
    if (!microAppName && window.top !== window && ((_c = window.top) == null ? void 0 : _c["microAppObj"])) {
      microAppName = (_e = (_d = window.top["microAppObj"]).getMicroAppNameByEntry) == null ? void 0 : _e.call(_d, window.location.pathname);
    }
    if (!import_iup_core.core.isNullOrEmpty(microAppName)) {
      const openMicroApp = ((_j = (_i = (_g = (_f = import_iup_core.core.external).getQianKun) == null ? void 0 : (_h = _g.call(_f)).getMasterInfo) == null ? void 0 : _i.call(_h)) == null ? void 0 : _j.openMicroApp) || (window.top || window)["openMicroAppByMaster"];
      const microAppPath = tmpPath.replace(`${subRoot}/${microAppName}/`, "/");
      if (openMicroApp) {
        openMicroApp({
          microAppName,
          microAppPath,
          query
        });
      } else {
        (0, import_iup_core.getHistory)().push(import_iup_core.core.handleUrl(microAppPath, query));
        console.warn(`当前环境不支持微应用方式打开:sub/${microAppName}${microAppPath}，请集成到微前端框架中使用!`);
      }
      return true;
    }
    if (/^https?:/.test(path)) {
      openUrl(import_iup_core.core.handleUrl(path, query), query == null ? void 0 : query.appTitle);
      return true;
    }
    const hash = window.location.hash;
    const newPath = import_iup_core.core.handleUrl(tmpPath, query);
    if (hash) {
      const currentPath = hash.substring(1);
      if (newPath === currentPath) {
        return false;
      }
    }
    (0, import_iup_core.getHistory)().push(newPath);
    return true;
  },
  async close(check = false) {
    var _a, _b, _c;
    const closeCheck = ((_a = import_iup_core.core.getPageInstance()) == null ? void 0 : _a["$CloseCheck"]) || ((_c = (_b = import_iup_core.core.getPageInstance()) == null ? void 0 : _b.parent) == null ? void 0 : _c["$CloseCheck"]);
    if (check && import_iup_core.core.isFunction(closeCheck)) {
      const checkSuccess = await closeCheck(check === true ? {} : check);
      if (!checkSuccess) {
        return false;
      }
    }
    (0, import_iup_core.getHistory)().goBack();
    return true;
  },
  getUser() {
    return import_iup_core.core.getUser();
  },
  safeRefresh(query = {}) {
    var _a, _b, _c, _d;
    const refreshLoad = (_d = (_c = (_b = (_a = import_iup_core.core.external).getQianKun) == null ? void 0 : _b.call(_a)) == null ? void 0 : _c.history) == null ? void 0 : _d.reload;
    if (refreshLoad) {
      refreshLoad(query);
    } else {
      const { pathname } = (0, import_iup_core.getHistory)().location;
      const lastQuery = import_iup_core.core.getQueryValue("");
      const newQuery = { ...lastQuery, ...query };
      const pathUrl = window.location.pathname || "/";
      window.location.href = import_iup_core.core.handleUrl(`${pathUrl}#${pathname}`, newQuery);
      window.location.reload();
    }
  },
  updateTitle(title) {
    var _a, _b, _c, _d, _e, _f;
    const updateTitle = ((_d = (_c = (_b = (_a = import_iup_core.core.external).getQianKun) == null ? void 0 : _b.call(_a)) == null ? void 0 : _c.history) == null ? void 0 : _d.updateTitle) || ((_f = (_e = window.top) == null ? void 0 : _e["globalTabApis"]) == null ? void 0 : _f.updateTitle);
    if (updateTitle) {
      updateTitle(title);
    } else {
      document.title = title;
    }
  },
  openHelp(params) {
    var _a, _b;
    return (_b = (_a = import_iup_core.core.external).openHelp) == null ? void 0 : _b.call(_a, params);
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CommonUtil
});
